'use strict';
/**
 * Created by xgjiang on 2015-10-21.
 */
define([
        'angular',
        'adConfig',
        'app/modules/users/define',
        'app/modules/users/services/users'
    ],
    function (angular, adConfig) {
        angular.module('ad.users')
            .controller('adUsersRoleController', ['$scope', '$uibModalInstance', 'adUsersService', 'item', function ($scope, $uibModalInstance, service, item) {
                $scope.usr_id = item.user_id;
                $scope.u_roles = [];
                $scope.selected = [];
                $scope.selectedTags = [];
                $scope.loadRoles = function () {
                    service.getAllRoles().then(function (data) {
                        var obj = angular.fromJson(data);
                        if (obj.result == 'success') {
                            $scope.u_roles = obj.data.items;
                        }
                    });
                };
                $scope.loadUsrRoles = function () {
                    service.getUsrRoles($scope.usr_id).then(function (data) {
                        var obj = angular.fromJson(data);
                        if (obj.result == 'success') {
                            if (obj.data.length > 0) {
                                var role_ids = _.map(obj.data, function (row) {
                                    return row.role_id;
                                });
                                $scope.selected = role_ids;
                                angular.forEach($scope.selected, function (roleId) {
                                    $scope.isSelected(roleId);
                                });
                            }
                        }
                    });
                };

                $scope.loadRoles();
                $scope.loadUsrRoles();

                var updateSelected = function (action, id, name) {
                    if (action == 'add' && $scope.selected.indexOf(id) == -1) {
                        $scope.selected.push(id);
                        $scope.selectedTags.push(name);
                    }
                    if (action == 'remove' && $scope.selected.indexOf(id) != -1) {
                        var idx = $scope.selected.indexOf(id);
                        $scope.selected.splice(idx, 1);
                        $scope.selectedTags.splice(idx, 1);
                    }
                }

                $scope.updateSelection = function ($event, id) {
                    var checkbox = $event.target;
                    var action = (checkbox.checked ? 'add' : 'remove');
                    updateSelected(action, id, checkbox.name);
                }

                $scope.isSelected = function (id) {
                    return $scope.selected.indexOf(id) >= 0;
                }

                $scope.convertType = function (type) {
                    return _.findWhere(adConfig.userRoleTypes, {value: (type)}).text;
                }

                $scope.ok = function () {
                    var roleIds;
                    if ($scope.selected.length == 0) {
                        return;
                    }
                    roleIds = $scope.selected.join(',')
                    service.saveUsrRole($scope.usr_id, roleIds).then(function (data) {
                        $uibModalInstance.close(data);
                    });
                };

                $scope.cancel = function () {
                    $uibModalInstance.dismiss('cancel');
                };
            }]);
    });